package org.huawei260.TiKu.ODsuanfa;

import java.util.Arrays;
import java.util.Scanner;
public class A_2023A_3000__双指针_双十一 {

//	public class Main {
		// 输入获取
		public static void main(String[] args) {
			Scanner sc = new Scanner(System.in);

			Integer[] arrM =
				Arrays.stream(sc.nextLine().split(",")).map(Integer::parseInt).toArray(Integer[]::new);

			int r = Integer.parseInt(sc.nextLine());

			System.out.println(getResult(arrM, r));
		}

		// 算法入口
		public static int getResult(Integer[] arr, int target) {
			// 题目说小明要购买三件，如果商品不足三件直接返回-1
			if (arr.length < 3) return -1;

			// 数组升序
			Arrays.sort(arr);

			int ans = -1;

			for (int i = 0; i < arr.length; i++) {
				int l = i + 1;
				int r = arr.length - 1;

				while (l < r) {
					int sum = arr[i] + arr[l] + arr[r];
					if (sum == target) {
						return sum;
					} else if (sum < target) {
						ans = Math.max(ans, sum);
						l++;
					} else {
						r--;
					}
				}
			}

			return ans;
		}
	}